package com.bw.a2209clubshop.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.bw.a2209clubshop.base.state.Loading
import com.bw.a2209clubshop.base.state.RequestEvent
import com.bw.a2209clubshop.base.state.Success
import com.bw.a2209clubshop.observeWithLifecycle
import kotlinx.coroutines.flow.StateFlow

abstract class BaseFragment <VM:BaseViewModel,VDB:ViewDataBinding>:Fragment(){
    protected lateinit var mBinding:VDB
    protected lateinit var mParentView:View
    protected abstract val mLayoutResId:Int
    open val mPageCanBack:Boolean=true
    val mCanBack:Boolean
        get(){
            //可返回
            if(mPageCanBack){
                return true
            }
            //不可返回
            val fragments = childFragmentManager.fragments //当前fragment是否有子fragment
            if(fragments.isEmpty()){
                return false
            }
            for (fragment in fragments) {
                if(fragment is BaseFragment<*,*>){
                    if(fragment.isVisible){
                        return fragment.mPageCanBack
                    }
                }
            }
            return mPageCanBack
        }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(inflater,mLayoutResId,container,false)
        mBinding.lifecycleOwner = viewLifecycleOwner
        mParentView = mBinding.root
        return mParentView
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        initListener()
    }
    open fun initView(){}
    open fun initData(){}
    open fun initListener(){}

    /**
     * 隐藏键盘
     */
    fun hideSoftKeyBroad(view:View){
        if(requireActivity() is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).hideKeyboard(view)
        }
    }

    /**
     * 显示键盘
     */
    fun showSoftKeyBroad(view:View){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).showSoftKeyBoard(view)
        }
    }

    open fun setLoadingViewVisible(show:Boolean, msg:String = ""){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).setLoadingViewVisible(show,msg)
        }
    }
    /**
     * 通用状态监听：Loading显示和隐藏、错误提示
     */
    open fun addCommonStateListener(state: StateFlow<RequestEvent>){
        state.observeWithLifecycle(this){
            when(it){
                is Loading -> {
                    setLoadingViewVisible(true)
                }
                is Error -> {
                    setLoadingViewVisible(false)
                    Toast.makeText(requireContext(),it.message, Toast.LENGTH_SHORT)
                }
                is Success -> {
                    setLoadingViewVisible(false)
                }
                else -> {
                }
            }
        }
    }
    open fun onLoadFailed(msg: String) {}
}